<template>
	<view class="page">
		<view class="search-box"><u-search placeholder="搜索" v-model="keyword" :show-action="false" @search="goSearch"></u-search></view>
		<view class="list"><escort-item v-for="(item, index) in list" :key="index" :isSelect="isSelect" :item="item" @click="bookTa"></escort-item></view>
		<!-- 可预约时间弹窗 -->
		<book-datetime ref="popDatetime" @ok="confirmBookDatetime"></book-datetime>
	</view>
</template>

<script>
import EscortItem from '@/components/escort-item/escort-item.vue';
import BookDatetime from '@/components/book-datetime/book-datetime.vue';
import { getEscortList } from '@/apis/request.js';

export default {
	components: {
		EscortItem,
		BookDatetime
	},
	data() {
		return {
			isSelect: false,
			keyword: '',
			page: 1,
			total: 0,
			list: [],
			serviceTime: '',
			bookEscort: {},
			specification: 0, //规格 0次 1半天 2半-白天 3半-夜间 4全天 5月
			isTreatmentTime: 1 //就诊时间 0否 1是
		};
	},
	onLoad(options) {
		this.isSelect = options.isSelect || false;
		this.specification = options.specification || 0;
		this.isTreatmentTime = options.isTreatmentTime || 1;
		this.serviceTime = options.serviceTime || '';
		this.init();
	},
	onPullDownRefresh() {
		this.init();
	},
	onReachBottom() {
		if (this.list.length < this.total) {
			this.getList();
		}
	},
	methods: {
		init() {
			this.page = 1;
			this.list = [];
			this.getList();
		},
		goSearch(value) {
			this.keyword = value;
			this.init();
		},
		getList() {
			let id = uni.getStorageSync('city').id;
			getEscortList({
				cityId: id,
				name: this.keyword,
				pageNo: this.page,
				pageSize: 10,
				status: ''
			})
				.then(res => {
					if (this.page == 1) {
						this.total = res.total;
						this.list = res.records;
					} else {
						this.list = this.list.concat(res.records);
					}
					if (this.list.length < this.total) {
						this.page++;
					}
				})
				.finally(() => {
					uni.stopPullDownRefresh();
				});
		},
		bookTa(item) {
			if (this.isTreatmentTime == 1) {
				this.bookEscort = item;
				this.$refs.popDatetime.open(item.id, '', this.specification);
			} else {
				uni.$emit('selectEscort', item);
				uni.navigateBack();
			}
		},
		confirmBookDatetime(e) {
			uni.$emit('selectEscort', this.bookEscort);
			uni.$emit('selectDatetime', e);
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	.search-box {
		padding: 20upx 30upx;
		background: white;
	}

	.list {
		background: white;
	}
}
</style>
